package com.study.sxc.controller;

import com.study.sxc.Entity.Machine;
import com.study.sxc.enums.ResponseCode;
import com.study.sxc.response.Response;
import com.study.sxc.service.machineService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@ResponseBody
@RequestMapping("/api/machine")
public class MachineController {

    @Resource
    private machineService machineService;

    @GetMapping("/get")
    public Response<List<Machine>> getMachines() {
        // 查询所有机器
        List<Machine> machines;
        try {
            machines = machineService.getAllMachines();
            System.out.println("查询结果: " + machines);
        } catch (Exception e) {
            System.out.println("查询异常: " + e.getMessage());
            e.printStackTrace();
            return Response.<List<Machine>>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info("查询异常: " + e.getMessage())
                    .data(null)
                    .build();
        }
        if(machines == null || machines.isEmpty()) {
            return Response.<List<Machine>>builder()
                    .code(ResponseCode.EMPTY_RESULT.getCode())
                    .info(ResponseCode.EMPTY_RESULT.getInfo())
                    .data(machines)
                    .build();
        }
        return Response.<List<Machine>>builder()
                .code(ResponseCode.SUCCESS.getCode())
                .info(ResponseCode.SUCCESS.getInfo())
                .data(machines)
                .build();
    }

    @PostMapping("/add")
    public Response<Void> addMachine(@RequestBody Machine machine) {
        System.out.println("[后端] /add 收到请求: " + machine);
        try {
            int result = machineService.addMachine(machine);
            System.out.println("[后端] /add service结果: " + result);
            if (result > 0) {
                return Response.<Void>builder().code("200").info("添加成功").build();
            } else {
                return Response.<Void>builder().code("500").info("添加失败").build();
            }
        } catch (Exception e) {
            System.out.println("[后端] /add 异常: " + e.getMessage());
            return Response.<Void>builder().code("500").info("添加异常: " + e.getMessage()).build();
        }
    }

    @PutMapping("/update")
    public Response<Void> updateMachine(@RequestBody Machine machine) {
        System.out.println("[后端] /update 收到请求: " + machine);
        try {
            int result = machineService.updateMachine(machine);
            System.out.println("[后端] /update service结果: " + result);
            if (result > 0) {
                return Response.<Void>builder().code("200").info("修改成功").build();
            } else {
                return Response.<Void>builder().code("500").info("修改失败").build();
            }
        } catch (Exception e) {
            System.out.println("[后端] /update 异常: " + e.getMessage());
            return Response.<Void>builder().code("500").info("修改异常: " + e.getMessage()).build();
        }
    }

    @DeleteMapping("/delete")
    public Response<Void> deleteMachine(@RequestBody Machine machine) {
        System.out.println("[后端] /delete 收到请求: " + machine);
        try {
            Integer id = machine.getMachineId();
            int result = machineService.deleteMachine(id);
            System.out.println("[后端] /delete service结果: " + result);
            if (result > 0) {
                return Response.<Void>builder().code("200").info("删除成功").build();
            } else {
                return Response.<Void>builder().code("500").info("删除失败").build();
            }
        } catch (Exception e) {
            System.out.println("[后端] /delete 异常: " + e.getMessage());
            return Response.<Void>builder().code("500").info("删除异常: " + e.getMessage()).build();
        }
    }

    @GetMapping("/get/{id}")
    public Response<Machine> getMachineById(@PathVariable Integer id) {
        // 根据id获取机器信息
        Machine machine = machineService.getMachineById(id);
        if (machine == null) {
            return Response.<Machine>builder()
                    .code(ResponseCode.EMPTY_RESULT.getCode())
                    .info(ResponseCode.EMPTY_RESULT.getInfo())
                    .data(null)
                    .build();
        }
        return Response.<Machine>builder()
                .code(ResponseCode.SUCCESS.getCode())
                .info(ResponseCode.SUCCESS.getInfo())
                .data(machine)
                .build();
    }
} 